package com.yc.login;

import com.yc.login.bean.LoginProperties;
import com.yc.login.web.LoginInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@ComponentScan("com.yc.login")
@ConditionalOnProperty(prefix = "login", value = "enable")
@ConditionalOnClass(DataSource.class)
public class LoginConfig implements WebMvcConfigurer {

    @Resource
    DataSource dataSource;

    @Bean
    JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource);
    }

    @Resource
    LoginProperties loginProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(loginProperties.getInterceptors().toArray(new String[0]));
    }
}
